﻿<%@ WebHandler Language="C#" Class="Tag" %>

using System;
using System.Web;
using System.Text;
using Company.StudyClub.BLL.Common;
using Company.StudyClub.BLL.Tag;
using System.Collections.Generic;
using Company.StudyClub.Model;

public class Tag : IHttpHandler
{
    private HttpHelper helper = new HttpHelper();
    public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;
        switch (helper.GetString("type"))
        {
            case "searchtags":
                SearchTags(response);
                break;
            case "createtag":
                CreateTag(response);
                break;
            default: break;
        }
    }

    private void SearchTags(HttpResponse response)
    {       
        TagBLL objBLL = new TagBLL();

        List<Tag_Info> objList = objBLL.SearchTags(helper.GetString("keyword"), 1);

        StringBuilder objSb = new StringBuilder();

        objSb.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            Tag_Info model = (Tag_Info)objList[i];

            objSb.Append("{\"tid\":\"");
            objSb.Append(model.TagID);
            objSb.Append("\",\"tname\":\"");
            objSb.Append(model.TagName);
            objSb.Append("\"}");

            if (i != objList.Count - 1)
            {
                objSb.Append(",");
            }
        }

        objSb.Append("]");

        response.Write(objSb.ToString());
    }

    private void CreateTag(HttpResponse response)
    {
        TagBLL objBLL = new TagBLL();

        Tag_Info model = new Tag_Info();
        model.TagName = helper.GetString("tname");
        model.CreatorID = helper.CurrentUser.UserID;
        model.Status = 1;
        int tagId = objBLL.Add(model);

        response.Write(tagId);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}